/**
 * Created by howiezhang on 16/10/19.
 */
(function () {

	'use strict';

	angular
		.module('app')
		.controller('OrderReviewController', OrderReviewController);

	OrderReviewController.$inject = ['$scope', '$http', '$window', '$timeout', '$location', '$state', '$stateParams', 'API', 'ENUM'];

	function OrderReviewController($scope, $http, $window, $timeout, $location, $state, $stateParams, API, ENUM) {

		var order_id = $stateParams.order_id;

		$scope.order = {
            order_id: order_id
		};

		$scope.isLoading = false;
		$scope.isLoaded = false;
		$scope.isAnonymous = 0;

		$scope.touchGood = function (goods) {
            if (!goods.review) {
                goods.review = {};
            }
            goods.review.relation_id = goods.relation_id;
            goods.review.level = 3;
            if (!goods.review.content) {
                goods.review.content = "";
            }
        };

		$scope.touchMedium = function (goods) {
            if (!goods.review) {
                goods.review = {};
            }
            goods.review.relation_id = goods.relation_id;
            goods.review.level = 2;
            if (!goods.review.content) {
                goods.review.content = "";
            }
        };

		$scope.touchBad = function (goods) {
            if (!goods.review) {
                goods.review = {};
            }
            goods.review.relation_id = goods.relation_id;
            goods.review.level = 1;
            if (!goods.review.content) {
                goods.review.content = "";
            }
        };

		$scope.touchSetAnonymous = function () {
            $scope.isAnonymous = !$scope.isAnonymous;
        };

		$scope.touchSubmit = function () {
            var review = [];
            for (var i = 0; i < $scope.order.order_goods.length; i++) {
                var goods = $scope.order.order_goods[i];
                if (goods.review && goods.review.level) {
                    review.push(goods.review);
                }
            }

            if (review.length == 0) {
                $scope.toast('请选择商品评价');
                return;
            }

            var params = {};
            params.order_id = order_id;
            params.comment = JSON.stringify(review);
            if($scope.isAnonymous){
                params.is_anonymous = 1;
            } else {
                params.is_anonymous = 0;
            }

            API.order.comment(params).then(function (res) {
                if (res) {
                    $state.go('review-success', {
                        order_id: $scope.order.order_id
                    });
                }
            })
        };

		function _reload() {

			$scope.isLoading = true;
			$scope.isLoaded = false;

			API.order.getOne({
				order_id: order_id,
			}).then(function (order) {
				$scope.order = order;
				$scope.isLoading = false;
				$scope.isLoaded = true;
			});
		}

		_reload();
	}

})();